package com.googlecode.trabhci2011.stockmarket.server;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.Vector;

import java.util.HashMap;

//import com.google.gwt.core.client.GWT;
import com.googlecode.trabhci2011.stockmarket.shared.StockDataEntry;

public class StockDataReader {
	
	public static final HashMap<String, String> fileNames = new HashMap<String, String>();
	static {
		fileNames.put("DOW", "dowJones.csv");
		fileNames.put("SANDP", "SandP500.csv");
		fileNames.put("DAX", "dax.csv");
		fileNames.put("FTSE", "ftse100.csv");
		fileNames.put("NASDAQ", "nasdaq.csv");
		fileNames.put("CAC40", "cac40.csv");
		fileNames.put("BND10YR", "10YrBonds.csv");
	}
	
	public Vector<StockDataEntry> readDataFile(String indexName){
		Vector<StockDataEntry> stockData = new Vector<StockDataEntry>();
		BufferedReader r;
		try {			
			String filename = "Data/" + fileNames.get(indexName);
			System.out.println("filename:" + filename);
			r = new BufferedReader(new FileReader(new File(filename)));
			String line = r.readLine();
			while ((line=r.readLine()) != null) {
				line = indexName + "," + line;
				stockData.add(new StockDataEntry(line));
			};
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return stockData;		
	}
	
	public StockDataReader(HashMap<String, Vector<StockDataEntry>> stockData) {
		Iterator<String> it = fileNames.keySet().iterator();
		while (it.hasNext()){
			String name = it.next();
			
			System.out.println("LOADING:" + name);
			Vector<StockDataEntry> data = readDataFile(name);
			if (data != null) {
				stockData.put(name, data);
			}
		}
	}
	
}
